package config

import (
	"fmt"
	"os"
)

// GetDSN 获取数据库连接字符串
func GetDSN() string {
	host := getEnv("DB_HOST", "localhost")
	user := getEnv("DB_USER", "root")
	password := getEnv("DB_PASS", "----")
	dbname := getEnv("DB_NAME", "plastic_company")
	port := getEnv("DB_PORT", "3306")

	return fmt.Sprintf("%s:%s@tcp(%s:%s)/%s?charset=utf8mb4&parseTime=True&loc=Local",
		user, password, host, port, dbname)
}

// getEnv 获取环境变量，如果不存在则返回默认值
func getEnv(key, defaultValue string) string {
	if value := os.Getenv(key); value != "" {
		return value
	}
	return defaultValue
}
